<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>
 
<script>
import { toRef } from 'vue'
export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: { // v-model默认绑定的值为modelValue
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    const checked = toRef(props, 'modelValue') // 定义checked存储接收到的boolean值
    const changeChecked = () => {
      emit('update:modelValue', !checked.value) // 给使用的父组件传值，实现复选框的勾选
    }
    return { checked, changeChecked }
  }
}
</script>
 
<style scoped lang="less"> 
 // 样式可以酌情更改
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>